package cn.lhx.blog.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.lhx.blog.formbean.MusicFormBean;
import cn.lhx.blog.plugin.flashPlayer.PlayList;
import cn.lhx.blog.plugin.flashPlayer.PlayerHelper;
import cn.lhx.blog.plugin.flashPlayer.Track;



public class MusicManager extends DispatchAction{
private String listLoaction;

	private String getPlayListLoaction(HttpServletRequest request){
		System.out.println(request.getSession().getServletContext().getRealPath("/content"));
		String contentPath=request.getSession().getServletContext().getRealPath("/content");
		contentPath+="/fmp-jw-files/playlists/fmp_jw_widget_playlist.xml";
		return contentPath;
	}

	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
				PlayList playList=PlayerHelper.loadPlayList(getPlayListLoaction(request));
		request.setAttribute("playList", playList);
	
		return mapping.findForward("loadOK");

	}
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Track track=new Track();
		MusicFormBean mf=(MusicFormBean)form;
		track.setAnnotation(mf.getAnnotation());
		track.setInfo(mf.getInfo());
		track.setLocation(mf.getLocation());
		track.setImage(mf.getImage());
		//获得播放列表
		String playListLoaction=getPlayListLoaction(request);
		PlayList playList=PlayerHelper.loadPlayList(playListLoaction);
		playList.getTrackList().get(0).getTrack().add(track);
		//保存列表
		PlayerHelper.savePlayList(playList, playListLoaction);
		return mapping.findForward("addOK");
	}
	
	//删除歌曲
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MusicFormBean mf=(MusicFormBean)form;
		System.out.println("mf.id = "+mf.getId());
		//获得播放列表
		String playListLoaction=getPlayListLoaction(request);
		PlayList playList=PlayerHelper.loadPlayList(playListLoaction);
		playList.getTrackList().get(0).getTrack().remove(mf.getId());
		//保存列表
		PlayerHelper.savePlayList(playList, playListLoaction);
		return mapping.findForward("addOK");
	}
	//更新歌曲
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		MusicFormBean mf=(MusicFormBean)form;
		System.out.println("mf.id = "+mf.getId());
		//获得播放列表
		String playListLoaction=getPlayListLoaction(request);
		PlayList playList=PlayerHelper.loadPlayList(playListLoaction);
		Track target=playList.getTrackList().get(0).getTrack().get(mf.getId());
		target.setAnnotation(mf.getAnnotation());
		target.setInfo(mf.getInfo());
		target.setLocation(mf.getLocation());
		target.setImage(mf.getImage());
		//保存列表
		PlayerHelper.savePlayList(playList, playListLoaction);
		
		return mapping.findForward("addOK");
	}
}
